package com.xique.park.service.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.request.web.subDev.ParkSubDevListRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xique.park.mapper.ParkDeviceSubMapper;
import com.xique.park.bean.domain.ParkDeviceSub;
import com.xique.park.service.service.IParkDeviceSubService;

/**
 * 挂在主摄像机下的其他设备Service业务层处理
 *
 * @author xique
 * @date 2021-12-09
 */
@Service
public class ParkDeviceSubServiceImpl implements IParkDeviceSubService {

    @Autowired
    private ParkDeviceSubMapper parkDeviceSubMapper;

    /**
     * 查询挂在主摄像机下的其他设备
     *
     * @param id 挂在主摄像机下的其他设备ID
     * @return 挂在主摄像机下的其他设备
     */
    @Override
    public ParkDeviceSub selectParkDeviceSubById(Long id) {
        return parkDeviceSubMapper.selectById(id);
    }

    /**
     * 查询挂在主摄像机下的其他设备列表
     *
     * @param parkDeviceSub 挂在主摄像机下的其他设备
     * @return 挂在主摄像机下的其他设备
     */
    @Override
    public List<ParkDeviceSub> selectParkDeviceSubList(ParkDeviceSub parkDeviceSub) {
        LambdaQueryWrapper<ParkDeviceSub> wrapper = new LambdaQueryWrapper<>();
        if (parkDeviceSub.getParkId() != null) {
            wrapper.eq(ParkDeviceSub::getParkId, parkDeviceSub.getParkId());
        }
        if (parkDeviceSub.getLaneId() != null) {
            wrapper.eq(ParkDeviceSub::getLaneId, parkDeviceSub.getLaneId());
        }
        if (StrUtil.isNotBlank(parkDeviceSub.getStatus())) {
            wrapper.eq(ParkDeviceSub::getStatus, parkDeviceSub.getStatus());
        }
        return parkDeviceSubMapper.selectList(wrapper);
    }

    @Override
    public IPage<ParkDeviceSub> selectParkSubDevPage(ParkSubDevListRequest search) {
        QueryWrapper<ParkDeviceSub> wrapper = new QueryWrapper<>();
        //TODO
       /* wrapper.eq(null != search.getParkId(),"park_id",search.getParkId());
        wrapper.like(StringUtils.isNotEmpty(search.getName()),"name",search.getName());
        wrapper.like(StringUtils.isNotEmpty(search.getIp()),"ip",search.getIp());
        wrapper.like(StringUtils.isNotEmpty(search.getDevCode()),"code",search.getDevCode());
        wrapper.eq(StringUtils.isNotEmpty(search.getModel()),"model",search.getModel());*/

        wrapper.eq(null != search.getParkId(), "park_id", search.getParkId());
        wrapper.like(StringUtils.isNotEmpty(search.getIp()), "ip", search.getIp());
        wrapper.le(null != search.getUpdateTime(), "update_time", search.getUpdateTime());

        /* 分页处理 */
        Page<ParkDeviceSub> page = new Page<>();
        if (null != search.getPageNum() && null != search.getPageSize()) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }
        wrapper.orderByDesc("update_time");
        return parkDeviceSubMapper.selectPage(page, wrapper);
    }

    /**
     * 新增挂在主摄像机下的其他设备
     *
     * @param parkDeviceSub 挂在主摄像机下的其他设备
     * @return 结果
     */
    @Override
    public int insertParkDeviceSub(ParkDeviceSub parkDeviceSub) {
        parkDeviceSub.setCreateTime(DateUtils.getNowDate());
        return parkDeviceSubMapper.insert(parkDeviceSub);
    }

    /**
     * 修改挂在主摄像机下的其他设备
     *
     * @param parkDeviceSub 挂在主摄像机下的其他设备
     * @return 结果
     */
    @Override
    public int updateParkDeviceSub(ParkDeviceSub parkDeviceSub) {
        parkDeviceSub.setUpdateTime(DateUtils.getNowDate());
        return parkDeviceSubMapper.updateById(parkDeviceSub);
    }

    /**
     * 批量删除挂在主摄像机下的其他设备
     *
     * @param ids 需要删除的挂在主摄像机下的其他设备ID
     * @return 结果
     */
    @Override
    public int deleteParkDeviceSubByIds(List<Long> ids) {
        return parkDeviceSubMapper.deleteBatchIds(ids);
    }

    /**
     * 删除挂在主摄像机下的其他设备信息
     *
     * @param id 挂在主摄像机下的其他设备ID
     * @return 结果
     */
    @Override
    public int deleteParkDeviceSubById(Long id) {
        return parkDeviceSubMapper.deleteById(id);
    }
}
